package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.adpater.CollectionAdapter;
import where2go.sctu.com.zhiyou.db.ScrollLinearLayoutManager;
import where2go.sctu.com.zhiyou.entity.CollectionData;
import where2go.sctu.com.zhiyou.utils.HeaderStringRequest;
import where2go.sctu.com.zhiyou.utils.ShareUtils;

/**
 * Created by lenovo on 2018/5/21.
 */

public class MyCollectionActivity extends AppCompatActivity{
    private ImageView imageView;

    private RecyclerView mRecyclerView;
    private List<CollectionData.DataBean> dataBeanList=new ArrayList<CollectionData.DataBean>();
    private CollectionAdapter collectionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_collection_fragment);

        initView();

    }

    private void initView() {
        imageView=(ImageView) findViewById(R.id.collection);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRecyclerView=(RecyclerView) findViewById(R.id.collection_recyclerView);
        mRecyclerView.setLayoutManager(new ScrollLinearLayoutManager(this,
                ScrollLinearLayoutManager.VERTICAL,false));
        getMyCollection();
    }

    private void getMyCollection(){
        String url="http://47.104.80.155/youyiyou/sc/show?scType=0";
        HeaderStringRequest request=new HeaderStringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("TAG", "成功获取收藏数据");
                Gson gson=new Gson();
                CollectionData collectionData=gson.fromJson(response,CollectionData.class);
                dataBeanList=collectionData.getData();
                collectionAdapter=new CollectionAdapter(MyCollectionActivity.this,dataBeanList);
                collectionAdapter.setItemClickListener(new CollectionAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //跳转到详情页
                        Intent intent=new Intent();
                        intent.setClass(MyCollectionActivity.this,
                                LineDetailsActivity.class);
                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        int classId=dataBeanList.get(position).getAClass().getClassId();
                        intent.putExtra("classId",classId);
                        bundle.putString("classId", String.valueOf(classId));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
                mRecyclerView.setAdapter(collectionAdapter);
                collectionAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.toString());
                Toast.makeText(MyCollectionActivity.this, "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> map=new HashMap<String,String>();
                map.put("authorization", ShareUtils.getString(MyCollectionActivity.this,
                        "token",null));
                return map;
            }
        };
        MyApplication.getHttpQueues().add(request);
    }
}
